package com.example.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//日期工具类

public class DateUtil {

    public static Date createDate(int year,int month , int date){
        Calendar cal = Calendar.getInstance();
        cal.set(year,month-1,date);

        //Calendar类型是转换成Date类型
        //提供的Date getTime();
        return cal.getTime();
    }


    /**
     * 将字符串解析成日期
     * @param strDate
     * @param pattern
     * @return
     */
    public static Date parse(String strDate, String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String format(Date date,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String strDate = sdf.format(date);
        return strDate;
    }

}
